package com.zzty.txj1.controller;

import com.zzty.txj1.entity.Group;
import com.zzty.txj1.entity.User;
import com.zzty.txj1.service.GroupService;
import com.zzty.txj1.service.UserService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/group")
public class GroupController {
    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(GroupController.class);

    @Autowired
    UserService userService;

    @Autowired
    GroupService groupService;

//    @RequestMapping(value = "/getUser2ById", method = RequestMethod.POST)
//    @ResponseBody
//    public Map<String, Object> getUser2ById(@RequestBody User user) {
//        System.out.println("getUser2ById!!!!!!!!!!!");
//        Map<String, Object> map = new HashMap<>();
//        List<User> users = userService.getUser2ById(user);
//        if (users == null || users.isEmpty()) {
//            map.put("users", users);
//        } else {
//            map.put("users", null);
//        }
//        return map;
//    }

    @RequestMapping(value = "/setGroup/{userId1}/{userId2}/{clazzId}", method = RequestMethod.POST)
    public Map<String, String> setGroup(@PathVariable("userId1") Integer userId1, @PathVariable("userId2") Integer userId2, @PathVariable("clazzId") Integer clazzId) {
        Map<String, String> map = new HashMap<>();
        User user1 = userService.getUserById(userId1);
        User user2 = userService.getUserById(userId2);
        String userName1 = user1.getName();
        String userName2 = user2.getName();
        System.out.println("userName1: " + userName1 + " userName2: " + userName2);
        List<Group> groups = groupService.getGroup(clazzId);
        if (groups == null || groups.isEmpty()) {
            Integer count = groupService.setGroup(userName1, userName2, clazzId);
            if (count == 1) {
                map.put("result", "success");
            } else {
                map.put("result", "false");
            }
        } else {
            for (Group group : groups) {
                System.out.println("group: " + group.toString());
                if (userName1.equals(group.getUserName1()) || userName1.equals(group.getUserName2()) || userName2.equals(group.getUserName1()) || userName2.equals(group.getUserName2())) {
                    map.put("result", "exist");
                    break;
                }
            }
            if(map.get("result")!="exist"){
                Integer count = groupService.setGroup(userName1, userName2, clazzId);
                if (count == 1) {
                    map.put("result", "success");
                } else {
                    map.put("result", "false");
                }
            }
        }
        return map;
    }

    @RequestMapping("/inGroup/{clazzId}")
    public Map<String, Object> inGroup(@PathVariable("clazzId") Integer clazzId) {
        Map<String, Object> map = new HashMap<>();
        List<Group> groups = groupService.getGroup(clazzId);
        if (groups == null || groups.isEmpty()) {
            map.put("group", null);
        } else {
            map.put("group", groups);
        }
        return map;
    }

    @RequestMapping("/deleteGroup/{groupId}")
    public Integer deleteGroup(@PathVariable("groupId") Integer groupId) {
        Integer count = groupService.deleteGroup(groupId);
        System.out.println("count: " + count);
        if (count == 1) {
            return count;
        } else {
            count = 0;
            return count;
        }
    }

}
